package com.example.administrator.gynews;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.administrator.gynews.Activity.StudentInfoActivity;
import com.example.administrator.gynews.adapter.ClassMateListVIewAdapter;
import com.example.administrator.gynews.bean.ClassMate;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private List<ClassMate.ListBean> listBeen;
    private ListView classMateListVIew;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
        initView();

    }
    private void  initView(){
        classMateListVIew = (ListView) findViewById(R.id.classmate_listview);
        classMateListVIew.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(MainActivity.this, StudentInfoActivity.class);
                intent.putExtra("id",listBeen.get(position).getId()+"");
                startActivity(intent);
            }
        });

    }
    private void  initData(){
        String url = "http://10.1.63.140:8090/freshmenapp/classmate/listbyclassname";
        RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
        JSONObject jsonObject = new JSONObject();
        final Gson gson = new Gson();
        try {
            jsonObject.put("className","18移动1班");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                ClassMate classMate = gson.fromJson(jsonObject.toString(),ClassMate.class);
                listBeen = classMate.getList();
                classMateListVIew.setAdapter(new ClassMateListVIewAdapter(getApplicationContext(),listBeen));

                //Toast.makeText()
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

                Toast.makeText(MainActivity.this,"网络错误",Toast.LENGTH_SHORT).show();
            }
        });
        requestQueue.add(jsonObjectRequest);
    }
}
